package com.blb.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.entity.Menu;
import com.blb.entity.User;
import com.blb.mapper.MenuExtMapper;
import com.blb.service.MenuService;
import com.blb.utils.DateUtils;
import com.blb.utils.NowUserUtils;
import com.blb.utils.UUIDUtils;

@Controller
@RequestMapping("menu")
public class MenuController {


    @Autowired
    private MenuExtMapper menuExtMapper;

    @Autowired
    private MenuService menuService;

    /**
     * 根据用户id获取它所拥有的按钮
     *
     * @param model
     * @param httpsession
     * @param menuid
     * @return
     * @throws IOException
     */
    @RequiresPermissions("menu:list")
    @RequestMapping("showList")
    public String showList(Model model, HttpSession httpsession, String menuid) throws IOException {
        //查询左侧菜单
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Menu> list = menuExtMapper.getButtenByuserid(user.getId(), menuid);

        model.addAttribute("bts", list);
        return "/menu/menu_list";
    }

    /**
     * 显示菜单
     *
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("page")
    public List<Menu> page() throws IOException {
        List<Menu> list = menuService.page();
        return list;
    }

    /**
     * 获取父级菜单
     *
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("getParentMenu")
    public List<Map<String, Object>> getParentMenu() throws IOException {
        List<Map<String, Object>> parentMenu = menuService.getParentMenu();
        return parentMenu;
    }

    /**
     * 菜单添加
     *
     * @param menu
     * @param httpsession
     * @return
     */
    @RequiresPermissions("menu:add")
    @ResponseBody
    @RequestMapping("add")
    public String add(Menu menu, HttpSession httpsession) {
        menu.setId(UUIDUtils.getID());
        menu.setCreateTime(DateUtils.gettime());
        User user = NowUserUtils.getNowUser();
        menu.setCreateUser(user.getUsername());
        try {
            menuService.add(menu);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "";
    }

    /**
     * 修改
     *
     * @param menu
     * @param httpsession
     * @return
     */
    @RequiresPermissions("menu:update")
    @ResponseBody
    @RequestMapping("update")
    public String update(Menu menu, HttpSession httpsession) {
        menu.setUpdateTime(DateUtils.gettime());

        User user = NowUserUtils.getNowUser();
        menu.setUpdateUser(user.getUsername());
        try {
            menuService.update(menu);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequiresPermissions("menu:delete")
    @ResponseBody
    @RequestMapping("delete")
    public String delete(@RequestBody String id) {
        String msg = "";
        try {
            msg = menuService.delete(id);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return msg;
    }


}
